public class Solution {
    //字符中第一个不重复字符
    public int firstUniqChar(String s) {
        int n = s.length();
        //数组中存放字符出现次数
        int[] arr = new int[26];
        for(int i = 0; i < n; i++) {
            arr[s.charAt(i) - 'a']++;
        }
        //定义最早出现的索引
        int min = Integer.MIN_VALUE;
        for(int i = 0; i < 26; i++) {
            if(arr[i] == 1) {
                //找到该字符的位置
                int index = 0;
                for(int j = 0; j < n; j++) {
                    if(i +'a' == s.charAt(j)) {
                        index = j;
                    }
                }
                //比较找到最小索引
                if(min > index) {
                    min = index;
                }
            }
        }
        return min;
    }
    //二分查找
    public int search(int[] nums, int target) {
        return BinarySearch(0, nums.length-1, nums, target);
    }
    private int BinarySearch(int left, int right, int[] nums, int target) {
        int n = right + left;
        int mid = n / 2;
        //找不到对应target
        if ( left > right|| left == right && nums[left] != target) {
            return -1;
        }
        if (nums[mid] > target) {
            return BinarySearch(left, mid-1, nums, target);
        }else if (nums[mid] < target) {
            return BinarySearch(mid+1, right, nums, target);
        }else {
            return mid;
        }
    }
}
